/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dialogs.emprestimos;

import dao.Dao;
import dialogs.usuario.JDialogListarUsuario;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import libcontrol.model.Debito;
import libcontrol.model.Usuario;

/**
 *
 * @author filipe
 */
public class JDialogRegistrarPagamento extends javax.swing.JDialog {
    private Usuario usuario;
    /**
     * Creates new form JDialogRegistrarPagamento
     */
    public JDialogRegistrarPagamento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelUsuario = new javax.swing.JLabel();
        jTextFieldUsuario = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabelValorDebito = new javax.swing.JLabel();
        jLabelDepito = new javax.swing.JLabel();
        jButtonDebitar = new javax.swing.JButton();
        jButtonFechar = new javax.swing.JButton();
        jButtonBuscarUsuario = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabelUsuario.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabelUsuario.setText("Usuário:");

        jTextFieldUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldUsuarioActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Obra", "Débito"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);

        jLabelValorDebito.setFont(new java.awt.Font("Arial Black", 0, 24)); // NOI18N
        jLabelValorDebito.setText("R$ 0,00");

        jLabelDepito.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabelDepito.setText("Débito Total");

        jButtonDebitar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/debitar.png"))); // NOI18N
        jButtonDebitar.setText("Debitar");
        jButtonDebitar.setEnabled(false);
        jButtonDebitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDebitarActionPerformed(evt);
            }
        });

        jButtonFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/cancelar.png"))); // NOI18N
        jButtonFechar.setText("Fechar");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });

        jButtonBuscarUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/pesquisar.png"))); // NOI18N
        jButtonBuscarUsuario.setText("Buscar Usuario");
        jButtonBuscarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarUsuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(4, 4, 4)
                            .addComponent(jLabelUsuario)
                            .addGap(18, 18, 18)
                            .addComponent(jTextFieldUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(28, 28, 28)
                            .addComponent(jButtonBuscarUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabelDepito)
                            .addGap(29, 29, 29)
                            .addComponent(jLabelValorDebito, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addComponent(jButtonFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonDebitar, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 567, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelUsuario)
                    .addComponent(jTextFieldUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscarUsuario))
                .addGap(48, 48, 48)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDepito)
                    .addComponent(jLabelValorDebito, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonDebitar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldUsuarioActionPerformed
        jButtonBuscarUsuarioActionPerformed(evt);
    }//GEN-LAST:event_jTextFieldUsuarioActionPerformed

    private void jButtonDebitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDebitarActionPerformed
        List<Debito> debitosPendentes = usuario.getDebitosPendentes();
        for (Debito debito : debitosPendentes) {
            new Dao<Debito>(Debito.class).remove(debito.getId());
        }
        usuario.setDebitos(new ArrayList<Debito>());
    }//GEN-LAST:event_jButtonDebitarActionPerformed

    private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonFecharActionPerformed

    private void jButtonBuscarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarUsuarioActionPerformed
        new JDialogListarUsuario((JFrame) getParent(), true, this).setVisible(true);
        if (usuario != null) {
            jTextFieldUsuario.setText(usuario.getNome());
            List<Debito> debitosPendentes = usuario.getDebitosPendentes();
            if (debitosPendentes.isEmpty()) {                
                jButtonDebitar.setEnabled(false);
                JOptionPane.showMessageDialog(this, "Usuário não possui débitos!", "Erro", JOptionPane.ERROR_MESSAGE);
            } else {
                DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
                model.setNumRows(0);
                double total = 0;
                Object[] rowData = new Object[3];
                for (Debito debito : debitosPendentes) {
                    rowData[0] = debito.getDevolucao().getExemplar().getId();
                    rowData[1] = debito.getDevolucao().getExemplar().getObra().getTitulo();
                    rowData[2] = debito.getValor();
                    total += debito.getValor();
                    model.addRow(rowData);
                }
                jTable1.setModel(model);
                jLabelValorDebito.setText(String.format("R$ %.2f", total)); 
                jButtonDebitar.setEnabled(true);
            }
        }
    }//GEN-LAST:event_jButtonBuscarUsuarioActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscarUsuario;
    private javax.swing.JButton jButtonDebitar;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JLabel jLabelDepito;
    private javax.swing.JLabel jLabelUsuario;
    private javax.swing.JLabel jLabelValorDebito;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldUsuario;
    // End of variables declaration//GEN-END:variables

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }
}
